-
-
Notifications
You must be signed in to change notification settings - Fork 646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: Enhance Memory Management with Lock-Free Allocator, Preallocation, and Optimized Thread-Local Caching #2825
Open
beats-dh
wants to merge
8
commits into
main
Choose a base branch
from
lockerfree
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This comment was marked as outdated.
This comment was marked as outdated.
dudantas
reviewed
Sep 18, 2024
This PR is stale because it has been open 45 days with no activity. |
Quality Gate passedIssues Measures |
This PR is stale because it has been open 45 days with no activity. |
Quality Gate passedIssues Measures |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Detailed Description:
1. Introduction of Static Preallocation:
preallocate
Method: Added the ability to preallocate a fixed number of memory blocks (STATIC_PREALLOCATION_SIZE = 100
) during the initialization ofLockfreePoolingAllocator
. This reduces the need for dynamic allocation during runtime and improves system efficiency.std::call_once
: Ensures that preallocation is performed only once, safely, and at runtime usingstd::call_once
andstd::once_flag
.2. Optimization of Thread-Local Cache:
LOCAL_CACHE_LIMIT
): Introduced a dynamic calculation to adjust the size of the thread-local cache based on the number of threads available in the system (TOTAL_THREADS
). The local cache is adjusted to ensure that each thread has an adequate cache size, preventing excessive memory usage in environments with many threads. The default value isstd::max(35 / TOTAL_THREADS, 5)
.thread_local
): The local cache is stored in athread_local
variable, ensuring that each thread maintains its own separate cache, improving efficiency by avoiding contention.3. Improvements in the
allocate
Function:4. Improvements in the
deallocate
Function:Reason for Replacing
std::make_shared
with This New Implementation:1. Precise Control of Allocation and Deallocation:
The primary reason for replacing
std::make_shared
with the new implementation usingLockfreePoolingAllocator
is the need for finer and more efficient control over memory allocation and deallocation.std::make_shared
combines object and reference counter allocation into a single operation, which is efficient in terms of memory usage, but does not offer the flexibility required for a system that demands specific optimizations like thread-local caching and memory block preallocation.2. Optimization for Multithreaded Environments:
In a multithreaded environment, the new implementation allows each thread to maintain its own local memory block cache, reducing contention when accessing shared resources.
std::make_shared
, on the other hand, does not provide mechanisms to leverage these optimizations, making it less efficient in scenarios where frequent object allocation and deallocation occur.3. Reusing Memory Blocks:
With the new implementation, memory blocks can be reused from both a thread-local cache and a lock-free shared list, depending on availability. This not only improves performance by minimizing dynamic allocations, but also offers more predictable and efficient memory management, especially under high load.
4. Reduction of Memory Fragmentation:
By using the new allocation strategy, there is a significant reduction in memory fragmentation. This is due to the ability to preallocate and efficiently reuse memory blocks, something that
std::make_shared
does not allow in a granular manner.5. Flexibility and Extensibility:
The new approach also offers greater flexibility for future optimizations and adjustments according to the application's needs. The
LockfreePoolingAllocator
implementation allows customizations such as the amount of preallocated memory, the size of the local cache, and the behavior of the lock-free list, aspects that cannot be easily managed withstd::make_shared
.In summary, switching to this new implementation provides greater control and efficiency in memory management, improving application performance in environments where scalability and high performance are crucial.